/**
 * https://leetcode-cn.com/problems/linked-list-cycle-ii/
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function(head) {
    let f = head;
    let s = head;
    if (!head || !head.next) return null;
    while (f && f.next) {
        f = f.next.next;
        s = s.next;
        if (f === s) {
            f = head;
            while (f !== s) {
                f = f.next;
                s = s.next;
            }
            return s;
        }
    }
    return null;
};